package cn.org.timespace.ons.action.base;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.service.permission.EfUserLoginLogService;
import cn.org.timespace.easyframe.service.permission.OperationLogService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;
import cn.org.timespace.ons.service.base.MomService;

import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.util.pager.PagerHelper;
import cn.org.timespace.ons.vo.base.Mom;
import cn.org.timespace.ons.vo.base.Options;
import com.opensymphony.xwork2.ActionContext;

/**
 * 交换中心管理
 * @return
 * @throws Exception
 */

public class MomAction extends EFBaseAction{
	
	private static Log log = LogFactory.getLog(MomAction.class);
	private MomService momService;
	private OperationLogService operationLogService;
	public MomService getMomService() {
		return momService;
	}
	public void setMomService(MomService momService) {
		this.momService = momService;
	}
	public MomAction()
	{
		super(new MomModel());
	}
	
	
	
	MomModel monModel = (MomModel) this.getModel();
	
	
	
	/**
	 * 显示中间件列表
	 * @return
	 * @throws Exception
	 */
	public String doList() throws Exception{
		Pager p = PagerHelper.getPagerInstance(getRequest());
		if(monModel.getType()==null){
			monModel.setStatus(1);
		}
		List<Object> resList = momService.findByCondition(monModel,p);
		
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		return LIST;
	}
	
	/**
	 * 删除中间件信息
	 * @return
	 * @throws Exception
	 */
	public String doDelete() throws Exception{
		
		String momId = getRequest().getParameter("momId");
		Mom mom = momService.findById(momId);
		mom.setStatus(0);    //设置为下线
		momService.update(mom);
		//momService.delete(mom);
		
		//保存用户操作记录
		operationLogService.save("1", "1", "1", "交换中心设置为下线", getCurLogonIp(), getUserAccount());
		
		return doList();
	}
	
	/**
	 * 根据终端的Id来判断更新中间件或者保存中间件
	 * @return
	 */
	public String doFindById(){
		
		String momId = getRequest().getParameter("momId");
		if(!StringUtils.isEmpty(momId)){//更新
			List<Options> list = findByCode();
			Map request = (Map)ActionContext.getContext().get("request");
			request.put("list",list);
			Mom mom = momService.findById(momId);
			monModel.setMom(mom);
			return UPDATE;
		}
		List list = findByCode();
		Map request = (Map)ActionContext.getContext().get("request");
		request.put("list",list);
		return ADD;
	}
	
	/**
	 * 更新中间件信息
	 * @return
	 */
	public String doUpdate() throws Exception{
	
		Mom mom = monModel.getMom();
		//增加地区码
		String sheng =	mom.getSelA();//省
	    String shi =	mom.getSelB();//市
	    String qu = mom.getSelC();//县/区
	    String id = "";
	    if(qu!=null&&qu.length()>0){
	    	 id = qu;
	    }else if(shi!=null&&shi.length()>0){
	    	
	    	 id = shi;	
	    }else if(sheng!=null&&sheng.length()>0){
	    	 id = sheng;
	    }
	    if(id!=null&&id.length()>0){
	    	if(!mom.getDqM().equals(id)){
	    		mom.setDqM(id);
	    	}
	    	
	    }
		momService.update(mom);
		return doList();
	}
	
	/**
	 * 保存中间件信息
	 * @return
	 */
	public String doSave() throws Exception{
		
		Mom mom = monModel.getMom();
//		String momid = IdentityGenerator.getInstance().getmonId();
//		mom.setMomid(momid);
		//增加地区码
		String sheng =	mom.getSelA();//省
	    String shi =	mom.getSelB();//市
	    String qu = mom.getSelC();//县/区
	    String id = "";
	    if(qu!=null&&qu.length()>0){
	    	 id = qu;
	    }else if(shi!=null&&shi.length()>0){
	    	
	    	 id = shi;	
	    }else if(sheng!=null&&sheng.length()>0){
	    	 id = sheng;
	    }
	    if(id!=null&&id.length()>0){
	    	if(!mom.getDqM().equals(id)){
	    		mom.setDqM(id);
	    	}
	    	
	    }
	    mom.setCreateTime(new Date());
		momService.save(mom);
		//保存用户操作记录
		operationLogService.save("1", "1", "1", "交换中心设置为下线", getCurLogonIp(), getUserAccount());
		mom.setMomid("");
		mom.setCompCode("");
		mom.setCompRandom("");
		mom.setDeviceCode("");
		mom.setDeviceRandom("");
		mom.setIpAddress("");
		mom.setPort("");
		return doFindById();
	}
	
	/**
	 * 校验厂家编码与机器码,保证唯一性
	 * @return
	 */
	public String findCompCodeAndDeviceCode() throws Exception{
	    
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpServletResponse resp = ServletActionContext.getResponse();
	    //清除缓存
	    resp.setHeader("pragma", "no-cache");
		resp.setHeader("cache-control", "no-cache");
		String compCode = request.getParameter("compCode");
		String deviceCode = request.getParameter("deviceCode");
		List<Mom> list = this.momService.findCompCodeAndDeviceCode(compCode,deviceCode);
		getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out = resp.getWriter();
		String str = "IP地址或端口存在重复记录";
		if(list.size() > 0 ){  //大于0说明厂家编码与机器码已经存在
			out.println(str);
			out.flush();
			return null;
		}
		return null;
	}

	/**
	 * 根据machines查询设备类型
	 * @return
	 */
	public List<Options> findByCode(){
		
		List<Options> list = this.momService.findByCode("machines");
		return list;
	}
	
	public List findByTheId(String theId){
		
		return this.momService.findByTheId(theId);
	}
	public OperationLogService getOperationLogService() {
		return operationLogService;
	}
	public void setOperationLogService(OperationLogService operationLogService) {
		this.operationLogService = operationLogService;
	}
}
